<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="backLogin">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- 侧边栏导航菜单 -->
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <!-- :index后面只能跟字符串 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
               @click="activeSave('/'+subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view :ss="dog"></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activePath:'',
      dog:'狗'
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    // 当渲染完毕后执行的回调
    this.$nextTick(()=>{
    //   console.log('created1');
    // console.log(this.$refs.span)+'1';
    })
    // console.log('created');
    // console.log(this.$refs.span);
  },
  mounted(){
    // console.log('mounted');
    // console.log(this.$refs.span);
    
  },
  methods: {
    backLogin() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(this.menulist)
    },
    activeSave(activePath){
      this.activePath = activePath
      window.sessionStorage.setItem('activePath',activePath)
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #363c42;
  padding-left: 0;
  display: flex;
  justify-content: space-between;
  align-items: center; //让退出按钮不再上下贴边
  font-size: 20px;
  color: #fff;
  div {
    display: flex;
    align-items: center;
    span {
      padding-left: 20px;
    }
  }
}
.el-aside {
  background-color: #313743;
}
.el-main {
  background-color: #e9edf1;
}
.iconfont {
  margin-right: 10px;
}
.el-menu{
  border-right: none;
}
</style>